package com.huohuo.concurrent.zk;

import org.I0Itec.zkclient.ZkClient;

public abstract class ZkAbstractLock implements LockInterface {
    //获取锁资源
    abstract boolean tryLock();

    //锁等待
    abstract void waitLock();

    private static final String ZK_SERVER_CLIENT = "192.168.137.3:2181,192.168.137.3:2182,192.168.137.3:2183";

    //创建zk连接
    protected ZkClient zkClient = new ZkClient( ZK_SERVER_CLIENT );

    protected static final String path1 = "/lock1";

    protected static final String path2 = "/lock2";



    @Override
    public void getLock() {
        if (tryLock()){
            System.out.println("获得到锁了");
        }else {
            //等待
             waitLock();
             //重新获取锁资源
             getLock();//递归调用
        }
    }


}
